package com.seeTools.next.service.sys.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seeTools.next.api.entity.sys.SysOssConfig;
import com.seeTools.next.common.constant.Constants;
import com.seeTools.next.common.core.PageQuery;
import com.seeTools.next.common.tools.oss.constant.OssConstant;
import com.seeTools.next.common.tools.redis.CacheUtils;
import com.seeTools.next.common.tools.redis.RedisUtils;
import com.seeTools.next.mapper.sys.SysOssConfigMapper;
import com.seeTools.next.service.BaseServiceImpl;
import com.seeTools.next.service.sys.SysOssConfigService;
import com.seeTools.next.util.PageQueryHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 对象存储配置Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-05
 */
@Service
public class SysOssConfigServiceImpl extends BaseServiceImpl<SysOssConfigMapper, SysOssConfig> implements SysOssConfigService {


    @Resource
    private PageQueryHelper<SysOssConfig> pageQueryHelper;

    /**
     * 分页查询
     *
     * @param pageQuery 分页数据
     * @return 对象
     */
    @Override
    public Page<SysOssConfig> findList(PageQuery<SysOssConfig> pageQuery) {
        QueryWrapper<SysOssConfig> queryWrapper = getQueryWrapper(pageQuery.getParams());
        return dictionary(pageQueryHelper.pageInfo(this, pageQuery, queryWrapper));
    }


    /**
     * 查询数据
     *
     * @param model
     * @return
     */
    @Override
    public List<SysOssConfig> selectDataList(SysOssConfig model) {
        QueryWrapper<SysOssConfig> queryWrapper = getQueryWrapper(model);
        return dictionary(list(queryWrapper));
    }


    /**
     * 修改配置
     *
     * @return boolean
     */
    @Override
    public boolean edit(SysOssConfig model) {
        boolean b = updateOnLock(model);
        if (b) {
            init();
        }
        return b;
    }

    /**
     * 初始化配置
     */
    @Override
    public void init() {
        List<SysOssConfig> list = findAll();
        // 加载OSS初始化配置
        for (SysOssConfig config : list) {
            String configKey = config.getConfigKey();
            if ("0".equals(config.getStatus())) {
                RedisUtils.setCacheObject(OssConstant.DEFAULT_CONFIG_KEY, configKey);
            }
            CacheUtils.put(Constants.CacheKey.SYS_OSS_CONFIG, config.getConfigKey(), JSON.toJSONString(config));
        }
    }

    /**
     * 修改默认状态
     *
     * @return boolean
     */
    @Override
    public boolean changeStatus(SysOssConfig sysOssConfig) {
        boolean b = false;
        LambdaUpdateWrapper<SysOssConfig> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        if (Constants.DictType.DICT_NORMAL.equals(sysOssConfig.getStatus())) {
            lambdaUpdateWrapper.set(SysOssConfig::getStatus, Constants.DictType.DICT_NORMAL)
                    .eq(SysOssConfig::getId, sysOssConfig.getId());
            b = update(lambdaUpdateWrapper);
            if (b) {
                LambdaUpdateWrapper<SysOssConfig> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.set(SysOssConfig::getStatus, Constants.DictType.DICT_UN_NORMAL)
                        .eq(SysOssConfig::getStatus, Constants.DictType.DICT_NORMAL)
                        .ne(SysOssConfig::getId, sysOssConfig.getId());
                update(updateWrapper);
            }
        } else {
            lambdaUpdateWrapper.set(SysOssConfig::getStatus, Constants.DictType.DICT_UN_NORMAL)
                    .eq(SysOssConfig::getId, sysOssConfig.getId());
            b = update(lambdaUpdateWrapper);
        }
        if (b) {
            init();
        }
        return b;
    }


    /**
     * 获取查询条件
     *
     * @param model
     * @return
     */
    public QueryWrapper<SysOssConfig> getQueryWrapper(SysOssConfig model) {
        QueryWrapper<SysOssConfig> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<SysOssConfig> lambdaQueryWrapper = queryWrapper.lambda();
        if (ObjectUtil.isNotEmpty(model)) {

        }
        return queryWrapper;
    }


    /**
     * 翻译数据
     *
     * @param page
     * @return
     */
    public Page<SysOssConfig> dictionary(Page<SysOssConfig> page) {
        if (ObjectUtil.isNotEmpty(page)) {
            List<SysOssConfig> list = page.getRecords();
            dictionary(list);
        }
        return page;
    }

    /**
     * 翻译
     *
     * @param list
     * @return
     */
    public List<SysOssConfig> dictionary(List<SysOssConfig> list) {
        if (CollUtil.isNotEmpty(list)) {
            list.forEach(item -> {
            });
        }
        return list;
    }

}

